package moni.anyou.com.view.view.my.systemset;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import org.json.JSONObject;
import org.kymjs.aframe.http.KJHttp;
import org.kymjs.aframe.http.KJStringParams;
import org.kymjs.aframe.http.StringCallBack;

import java.lang.reflect.Field;

import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseActivity;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.tool.AppTools;
import moni.anyou.com.view.widget.NetProgressWindowDialog;

/**
 * Created by Administrator on 2017/5/30.
 */

public class EditInfoActivity extends BaseActivity implements View.OnClickListener {
    private EditText etUnfo;
    private NetProgressWindowDialog window;
    private String strType = "";
    private String strTypeName = "";
    private String strValue = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editinfo);
        initState();
        initWindow();
        init();
    }

    @Override
    public void initView() {
        super.initView();
        initTitle();
        window = new NetProgressWindowDialog(mBaseActivity);
        Intent intent = getIntent();
        strType = intent.getStringExtra("type");
        strTypeName = intent.getStringExtra("typename");
       // try {
       //     strValue = Base64Util.getDecodeStr(intent.getStringExtra("value"));
       // }
       // catch(Exception ex){
            strValue = intent.getStringExtra("value");
        //}
        tvTitle.setText("修改"+strTypeName);
        tvRight.setVisibility(View.VISIBLE);
        //tvRight.setCompoundDrawables(null, null, null, null);
        tvRight.setText("修改");

        etUnfo = (EditText) findViewById(R.id.et_info);
        etUnfo.setText(strValue);
    }

    @Override
    public void setAction() {
        super.setAction();
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                onBack();
                break;
            case R.id.right_tv:
                postdata();
                break;
        }
    }

    //手机高版本样式
    private void initState() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

    }

    private SystemBarTintManager tintManager;
    //手机高版本样式
    @TargetApi(19)
    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(Color.parseColor("#FEC600"));

            tintManager.setStatusBarTintEnabled(true);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            try {
                Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);
                field.setInt(getWindow().getDecorView(),
                        Color.TRANSPARENT);
            } catch (Exception e) {
            }
        }

    }

    public void postdata() {
        if (etUnfo.getText().toString().equals("")){
            Toast.makeText(mContext, "不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        strValue = etUnfo.getText().toString().trim();
        KJHttp kjh = new KJHttp();
        KJStringParams params = new KJStringParams();
        String cmdPara = "{\"cmd\":\"6\",\"uid\":\""+ SysConfig.uid +"\",\"token\":\""+SysConfig.token + "\",\"type\":\""+ strType+"\",\"value\":\""+strValue+"\"}";
        params.put("sendMsg", cmdPara);
        window.ShowWindow();
        kjh.urlGet(SysConfig.ServerUrl, params, new StringCallBack() {
            @Override
            public void onSuccess(String t) {
                try {
                    JSONObject jsonObject = new JSONObject(t);
                    int result = Integer.parseInt(jsonObject.getString("result"));
                    if (result >= 1) {
                        //SysConfig.userInfoJson.put(strType,  Base64Util.getEncodeStr(strValue));
                        SysConfig.userInfoJson.put(strType, strValue);
                        SharedPreferences.Editor editor = SysConfig.prefs.edit();
                        editor.putString(strType, strValue);
                        editor.commit();
                        onBack();
                    } else {
                        if (result == -2) {
                            AppTools.jumptoLogin(mBaseActivity, result);
                        }
                        else {
                            Toast.makeText(mContext, jsonObject.get("retmsg").toString(), Toast.LENGTH_LONG).show();
                        }
                    }
                } catch (Exception ex) {
                    Toast.makeText(mContext, "数据请求失败", Toast.LENGTH_LONG).show();

                }
                window.closeWindow();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(mContext, "网络异常，请稍后再试", Toast.LENGTH_LONG).show();

                window.closeWindow();
            }
        });
    }


}
